<template>
  <div>
    <TableBar>
      <el-button @click="visible = true" type="primary" icon="el-icon-plus">添加</el-button>
      <el-button @click="onShowUpload" icon="el-icon-upload">批量上传</el-button>
      <el-button @click="onMulForbidden" icon="el-icon-lock">移除</el-button>
    </TableBar>

    <el-table :data="tableData" @selection-change="handleSelectionChange" style="width: 100%">
      <el-table-column fixed="left" type="selection" width="55"></el-table-column>
      <el-table-column fixed="left" type="index" width="55">序号</el-table-column>

      <el-table-column prop="userid" label="用户ID" ></el-table-column>
      <el-table-column prop="studentid" label="用户学号" ></el-table-column>

      <el-table-column prop="class_name" label="班级名称" ></el-table-column>

      <el-table-column prop="grade" label="年级" ></el-table-column>

      <el-table-column prop="major_name" label="专业名称" ></el-table-column>

      <el-table-column prop="college_name" label="学院名称" ></el-table-column>

      <el-table-column prop="school_name" label="学校名称" ></el-table-column>
    </el-table>
    <el-pagination
       background
      layout="prev, pager, next, sizes, jumper"
      @current-change="handleCurrentChange"
      @size-change=handleSizeChange
      :current-page.sync="search.page"
      :page-sizes="page_sizes"
      :page-size="search.page_size"
      :total="total"
    ></el-pagination>
    <el-dialog @close="onClose" :title="title" :visible.sync="visible">
      <div class="right-search-bar">
        <el-input placeholder="请按用户ID/学号/姓名查找" v-model="dSearch" />
        <el-button size="small" type="primary" @click="dialogSearch">查询</el-button>
      </div>
      <el-table :data="dTableData" @selection-change="dHandleSelectionChange" style="width: 100%">
        <el-table-column fixed="left" type="selection" width="55"></el-table-column>
        <el-table-column fixed="left" type="index" width="55">序号</el-table-column>
        <el-table-column prop="userid" label="用户ID" ></el-table-column>
        <el-table-column prop="studentid" label="用户学号" ></el-table-column>
        <el-table-column prop="class_name" label="班级名称" ></el-table-column>
        <el-table-column prop="grade" label="年级" ></el-table-column>
        <el-table-column prop="major_name" label="专业名称" ></el-table-column>
        <el-table-column prop="college_name" label="学院名称" ></el-table-column>
        <el-table-column prop="school_name" label="学校名称" ></el-table-column>
      </el-table>
      <el-button style="margin-left: 10px;" size="small" type="primary" @click="submitDialog">确定</el-button>
      <el-button style="margin-left: 10px;" size="small"  @click="onClose">取消</el-button>

    </el-dialog>
    <MulUpload :temp="6" :type="2" @onClose="onHideUpload" :show="showUpload" />
  </div>
</template>
<script>
import MulUpload from "@/components/MulUpload";
import CreateFilter from "@/mixins/CreateFilter";
import showEditMixin from "@/mixins/showEditMixin";
import uploadMixin from "@/mixins/uploadMixin";
import pageMixin from "@/mixins/pageMixin";
import { classModel, studentModel } from "@/api/models";
import TableBar from "@/components/TableBar";
import {Message} from 'element-ui';
export default {
  mixins: [CreateFilter, showEditMixin, uploadMixin, pageMixin],
  components: { MulUpload, TableBar },
  data() {
    return {
      tableData: [],
      total: 0,
      search: {
        page: 1,
        page_size: 10
      },
      title: '添加用户',
      visible: false,
      dTableData: [],
      dMultipleSelection: [],
      dSearch: null
    };
  },
  props: {
    class_id: [Number, String]
  },
  watch: {
    class_id() {
      this.getList();
    }
  },
  mounted() {
  },
  methods: {
    doForbidden(ids, status, msg) {
      this.$confirm(msg, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          classModel.batchAdd({
            class_id: this.search.class_id,
            classes: ids,
            action: status
          }, res => {
            this.$message({
              type: 'success',
              message: res.message 
            });
            this.getList();
          }, err => {console.log(err)})
        }).catch((err) => {
          console.error(err)
          this.$message({
            type: 'info',
            message: '已取消操作'
          });          
        });
    },
    onMulForbidden() {
      if(this.multipleSelection.length == 0) {
        return Message.warning('请选择要移除用户');
      }
      let ids = this.multipleSelection.map((v) => {
        return v.userid;
      })
      this.doForbidden(ids, 2, '确定批量移除用户？');
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    getList() {
      console.log("getList", this.class_id);
      studentModel.list(
        Object.assign({
          class_id: this.class_id
        }, this.search),
        res => {
          this.total = res.total_records;
          this.tableData = res.data.map(v => {
            v.classStatusLabel = this.classStatusToLabel(v.class_status);
            return v;
          });
        },
        err => {
          console.log(err);
        }
      );
    },
    dHandleSelectionChange(val) {
      this.dMultipleSelection = val;
    },
    dialogSearch() {
      studentModel.list({
        class_id: this.search.class_id,
        userid: this.dSearch
      }, res => {
          this.dTableData= res.data.map(v => {
            v.classStatusLabel = this.classStatusToLabel(v.class_status);
            return v;
          });
      })
    },
    submitDialog() {
      if(this.dMultipleSelection.length == 0) {
        return Message.warning('请选择要添加的用户');
      }
      let ids = this.dMultipleSelection.map((v) => {
        return v.userid;
      })
      this.doForbidden(ids, 1, '确定批量添加用户？');
    },
    onClose() {
      this.dSearch = null,
      this.visible = false
    }
  }
};
</script>
<style lang="scss">
</style>